package com.wl.zlmanage.controller;

import com.wl.zlmanage.bean.ApplyInfo;
import com.wl.zlmanage.bean.ApplySignInfo;
import com.wl.zlmanage.bean.ApprovalInfo;
import com.wl.zlmanage.bean.User;
import com.wl.zlmanage.constant.ActivityConstant;
import com.wl.zlmanage.mapper.ApplyMapper;
import com.wl.zlmanage.mapper.SignCustomMapper;
import com.wl.zlmanage.mapper.UserMapper;
import com.wl.zlmanage.utils.TimeUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by wanglei on 2018/5/25.
 */
@Controller
public class ApplyController {

    private Logger logger = LoggerFactory.getLogger(ApplyController.class);

    @Autowired
    ApplyMapper applyDao;

    @Autowired
    SignCustomMapper signCustomDao;

    @Autowired
    UserMapper userDao;

    @RequestMapping("/getApplySignList.do")
    @ResponseBody
    public Map<String, Object> getApplySignList(HttpSession session, @RequestParam("page") int page,
        @RequestParam("limit") int limit, @RequestParam("search") String search,
        @RequestParam("applyTypeId") int applyTypeId) {
        Map<String, Object> resultMap = new HashMap<>();
        String username = (String) session.getAttribute("username");
        Integer userId = (Integer) session.getAttribute("userId");
        int startIndex = (page - 1) * limit;
        User user = userDao.getUserById(username);
        int approvalState = user.getRoleId();
        List<ApplySignInfo> reList = applyDao
            .getApplySignList(startIndex, limit, search, approvalState, user, applyTypeId);
        Integer count = applyDao.getApplySignListCount(search, approvalState, user, applyTypeId);
        resultMap.put("data", reList);
        resultMap.put("count", count);
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        return resultMap;
    }

    @RequestMapping("/getApply.do")
    @ResponseBody
    public Map<String, Object> getApply(HttpSession session,
        @RequestParam("page") int page,
        @RequestParam("limit") int limit, @RequestParam("signId") int signId) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int startIndex = (page - 1) * limit;
        List<ApplyInfo> reList = applyDao.getApply(signId, startIndex, limit);
        Integer count = applyDao.getApplyCount(signId, startIndex, limit);
        Double sumPrice;
        for (ApplyInfo applyInfo : reList) {
            sumPrice = signCustomDao.getApplySumBySignId(applyInfo.getPersonSignId());
            applyInfo.setSumPrice(sumPrice);
        }
        resultMap.put("data", reList);
        resultMap.put("count", count);
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        return resultMap;
    }

    @RequestMapping("/getApplySign.do")
    @ResponseBody
    public ApplySignInfo getApplySign(@RequestParam("applySignId") int applySignId) {
        return applyDao.getApplySign(applySignId);
    }

    @RequestMapping("/addApplyInfo.do")
    @ResponseBody
    public String addApplyInfo(HttpSession session, ApplyInfo applyInfo) {
        applyInfo.setApplyState(1);
        applyInfo.setApplyTime(TimeUtils.getCurrentDateString(TimeUtils.DATE_MONTH_FORMAT_SQL));
        String createTime = TimeUtils.getCurrentDateString(TimeUtils.DATE_TIME_FORMAT);
        applyInfo.setApplyCreateTime(createTime);
        applyDao.addApplyInfo(applyInfo);
        if (null != applyInfo && applyInfo.getId() > 0) {
            Integer userId = (Integer) session.getAttribute("userId");
            ApprovalInfo ai = new ApprovalInfo();
            ai.setApprovalResult(ActivityConstant.APPROVAL_START);
            ai.setApprovalAdvice(ActivityConstant.APPROVAL_NULL);
            ai.setApprovalUserId(userId);
            ai.setSignId(applyInfo.getId());
            ai.setApprovalTime(TimeUtils.getCurrentDateString(TimeUtils.DATE_TIME_FORMAT));

            applyDao.saveApplyApprovalInfo(ai);
        }
        return "success";
    }

    @RequestMapping("/getAllApply.do")
    @ResponseBody
    public Double getAllApply(@RequestParam("signId") int signId) {
        Double tatal = applyDao.getAllApply(signId);
        return tatal;
    }

    @RequestMapping("/deleteApply.do")
    @ResponseBody
    public String deleteApply(@RequestParam("applyId") int applyId) {
        applyDao.deleteApply(applyId);
        return "success";
    }

    @RequestMapping("/doApplyApproval.do")
    @ResponseBody
    public String doApplyApproval(HttpSession session, @RequestParam("applySignId") int applySignId,
        @RequestParam("approvalState") int approvalState,
        @RequestParam("approvalResult") int approvalResult,
        @RequestParam("approvalAdvice") String approvalAdvice) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (approvalResult == 1) {
            if (approvalState == 2) {
                //部门总监审批完成后跳转到副总
                approvalState = 6;
            } else if (approvalState == 6) {
                //副总审批完成跳转到财务
                approvalState = 4;
            } else {
                approvalState += 1;
            }
            applyDao.askApplyApproval(applySignId, approvalState);
        } else {
            applyDao.askApplyApproval(applySignId, 0);
        }
        if(approvalState == 5){
            String applyTime = TimeUtils.getCurrentDateString(TimeUtils.DATE_MONTH_FORMAT_SQL);
            applyDao.updateApplyTime(applySignId,applyTime);
        }
        String approvalResultStr = ActivityConstant.APPROVAL_PASS;
        if (approvalResult == 0) {
            approvalResultStr = ActivityConstant.APPROVAL_BACK;
        }

        ApprovalInfo ai = new ApprovalInfo();
        ai.setApprovalResult(approvalResultStr);
        ai.setApprovalAdvice(approvalAdvice);
        ai.setApprovalUserId(userId);
        ai.setSignId(applySignId);
        ai.setApprovalTime(TimeUtils.getCurrentDateString(TimeUtils.DATE_TIME_FORMAT));

        applyDao.saveApplyApprovalInfo(ai);

        return "success";
    }

    @RequestMapping("/getApplyApprovalBySignId.do")
    @ResponseBody
    public List<ApprovalInfo> getApplyApprovalBySignId(
        @RequestParam("applySignId") int applySignId) {
        List<ApprovalInfo> reList = applyDao.getApplyApprovalBySignId(applySignId);
        return reList;
    }


}
